//
//  XFXTopicViewController.m
//  XFXnews
//
//  Created by dllo on 16/1/26.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "XFXTopicViewController.h"
#import "XFXHomePageTableViewCell.h"
#import "XFXTopicModel.h"
#import "XFXTopicDetaViewController.h"

@interface XFXTopicViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *allArray;

@end

@implementation XFXTopicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.allArray = [NSMutableArray array];
    
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.navigationItem.title = @"问吧";
    self.myTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    
    
    [self addHeader];
    [self addFooter];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;

    UINib *nib = [UINib nibWithNibName:@"XFXHomePageTableViewCell" bundle:[NSBundle mainBundle]];
    [self.myTableView registerNib:nib forCellReuseIdentifier:@"cell"];
    

}


- (void)addHeader{
    
    __block XFXTopicViewController *vc = self;
    [self.myTableView addHeaderWithCallback:^{
        vc.page = 0;
        vc.isUpLoading = NO;
        [vc getJson:vc.page];
    }];
    
    [self.myTableView headerBeginRefreshing];
}
- (void)addFooter{
    
    
    __block XFXTopicViewController *vc = self;
    
    [self.myTableView addFooterWithCallback:^{
        
        vc.page++;
        
        vc.isUpLoading = YES;
        [vc getJson:vc.page];
    }];
}

- (void)getJson:(NSInteger)page{
   
    
    NSString *str = [NSString stringWithFormat:@"http://c.3g.163.com/newstopic/list/expert/%ld-10.html",page*10];
    [XFXAppTools getNewsWithHeadline:str block:^(id reulst) {
        
        if (self.isUpLoading == NO) {
            [self.allArray removeAllObjects];
        }
        
        NSMutableDictionary *dic = [reulst objectForKey:@"data"];
        NSMutableArray *ListArray = [dic objectForKey:@"expertList"];
        for (NSDictionary *dic in ListArray) {
            XFXTopicModel *model = [[XFXTopicModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.allArray addObject:model];
            
        }
        [_myTableView reloadData];
        [_myTableView footerEndRefreshing];
        [_myTableView headerEndRefreshing];
    } error:^(NSError *error) {
        
        
    }];
    

    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.allArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XFXHomePageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model = self.allArray[indexPath.row];
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 300*XFXHEIGHT/736;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    XFXTopicDetaViewController *deta = [[XFXTopicDetaViewController alloc]init];
 
    XFXTopicModel *model = [self.allArray objectAtIndex:indexPath.row];
    deta.model = model;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:deta animated:YES];
    self.hidesBottomBarWhenPushed = NO;
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
